{extend name="public/base" /}
{block name="title"}推荐位{/block}
{block name="content"}
<div class="layui-fluid">   
  <div class="layui-card">   
    <div class="layui-card-body">
        <div class="layui-tab-content">
            <button class="layui-btn layui-btn-asuma layuiadmin-btn-admin" id="add">添加</button>
        </div>
   
        <table id="reco" lay-filter="reco" data-sturl="{:route('Shop/changeRecoWeight')}"></table>
    </div>
  </div>
</div>
{/block}
{block name="footjs"}
<script type="text/html" id="optTpl">
  <a class="layui-btn layui-btn-asuma layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-asuma layui-btn-xs" lay-event="detail">设置</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
  layui.config({
    base: '__JS__/layuiext/' //静态资源所在路径
  }).use([ 'table','form'], function(){
    var $ = layui.$
    ,form = layui.form
    ,table = layui.table;

   var tableIns =  table.render({
      elem: '#reco'
      ,url: "{:route('Shop/recoList')}" //数据接口
      ,page: true //开启分页
      ,limit:10
      ,limits:[10,25,50]
      ,defaultToolbar: ['filter', 'print']
      ,done:function (res, curr, count) {
        if(count <= this.limit){
          $('.layui-table-page').addClass('layui-hide');//总条数小于每页限制条数时隐藏分页
        }
      }
      ,cols: [[ //表头
        {field: 'name', title: '标题'}
        ,{field: 'sorted', title: '排序', edit : 'text'}
        ,{title: '操作',toolbar: '#optTpl'}
      ]]
    });
   
    table.on('sort(reco)',function(obj){
      if(tableIns.config.page.count <= tableIns.config.page.limit){
        $('.layui-table-page').addClass('layui-hide');//总条数小于每页限制条数时隐藏分页
      }
    })
    
    $('#add').on('click', function () {
         layer.open({
          type: 2
          ,title: '添加'
          ,content: "{:route('Shop/recoadd')}"
          ,maxmin: true
          ,area: ['350px', '180px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var submit = layero.find('iframe').contents().find("#submit");
            submit.click();
          }
        });
    });

    table.on('edit(reco)', function(obj){

      if(!/^(0|[1-9]+[0-9]*)$/.test(obj.value)){
        layer.tips('请输入正确的数值', obj.tr.find('td:eq(1)'), {tips:1});
        return false;  
      }

      $.post($('#reco').data('sturl'),{id: obj.data.id, newVal: obj.value}, function(res){
        if(res.code != 1){
          layer.tips(res.result, obj.tr.find('td:eq(1)'), {tips:1});
       
        }else{
          tableIns.reload()
        }
      }, 'json')
    })
    
    table.on('tool(reco)', function(obj){
        switch(obj.event){
          case 'edit':
              layer.open({
                type: 2
                ,title: '编辑'
                ,content: "{:route('recoEdit')}?id="+obj.data.id
                ,maxmin: true
                ,area: ['350px', '180px']
                ,btn: ['确定', '取消']
                ,success: function (layero, index) {
                   layero.find('input[name=id]').val(obj.data.id)
                }
                ,yes: function(index, layero){
                  var submit = layero.find('iframe').contents().find("#submit");
                  submit.click();
                }
              });
          break;
          case 'detail':
              layer.open({
                type: 2
                ,title: '设置'
                ,content: "{:route('Shop/recoDetail')}?id="+obj.data.id
                ,maxmin: true
                ,area: ['90%', '90%']
                ,success: function (layero, index) {
                   
                }
              });
          break;
          case 'del':
              layer.confirm('确定删除？？', function(index){
     
                   $.post("{:route('Shop/recoDel')}", {id:obj.data.id}, function(res){
                      if(res.code == 1){
                        layer.msg('删除成功');
                        table.reload('reco'); //重载表格
                        layer.close(index); //再执行关闭 
                      }else{
                        layer.msg(res.result, {icon: 5});
                      }
                    },'json');

                }); 
          break;
        };
    });


  });
</script>
{/block}